<?php
/**
 * Prado Portal.
 *
 * @author Steen Rabol <steen.rabol@gmail.com>
 * @link http://www.pradoportal.dk/
 * @copyright Copyright &copy; 2006,2007,2008 Steen Rabol
 * @license http://www.pradoportal.dk
 * @version $Id: LanguageImport.php 361 2010-02-03 08:08:44Z steen.rabol@gmail.com $
 *
 */

Prado::using('Application.Common.Data.PortalLanguageRecord');
Prado::using('Application.Common.Data.PortalLanguageStringRecord');

class LanguageImport extends PortalAdminPage
{
	private $_langid;
	private $_culture;

	public static function ToolMenuName()
	{
		return Prado::localize('Import language');
	}

	public function onLoad($param)
	{
		$this->_langid		= TPropertyValue::ensureInteger($this->Request['langid']);

		parent::onLoad($param);

		$this->setAdminPageTitle('Import language');
		$this->FileUpload->setAttribute("size",30);

		if(!$this->IsPostBack)
		{
			$this->FileUpload->MaxFileSize	= PortalUtil::return_bytes(ini_get('upload_max_filesize'));
			$this->ltFileSize->Text			= ini_get('upload_max_filesize');
		}
	}

	public function onFileUpload($sender,$param)
	{
		if($sender->HasFile)
		{
			PortalUtil::CreateDirStructure($this->Application->PortalBasePath	. '/storage/tmp');

			$savepath		= $this->Application->PortalBasePath	. '/storage/tmp/' . $sender->FileName;
			$sender->saveAs($savepath);
			$langstrings	= $this->loadlangfile($savepath);
			$langrow		= PortalLanguageRecord::finder()->findByculture($this->_culture);

			if(TPropertyValue::ensureInteger($this->DeleteFirst->Checked))
			{
				$sql = "delete from tbllangstrings where culture='" . $langrow->culture . "'";
				try
				{
					$this->DbConnection->createCommand($sql)->execute();
				}
				catch(Exception $e)
				{
					$this->reportError(1,$e->getMessage());
				}
			}

			foreach($langstrings as $langstring)
			{
				$lrec = PortalLanguageStringRecord::finder()->findByid_AND_culture($langstring[0],$langrow->culture);
				if(!$lrec)
				{
					$lrec	= new PortalLanguageStringRecord();
					$lrec->culture	= $langrow->culture;
					$lrec->lkey		= $langstring[1];
				}

				$save = false;
				if($lrec->fromvalue	!= $langstring[2])
				{
					$lrec->fromvalue	= $langstring[2];
					$save	= true;
				}

				if($lrec->tovalue != $langstring[3])
				{
					$lrec->tovalue		= (isset($langstring[3]) ? $langstring[3] : $lrec->fromvalue);
					$save	= true;
				}

				if($save)
				{
					if(strlen($lrec->tovalue) == 0)
					{
						$lrec->tovalue	= $lrec->fromvalue;
					}
					$lrec->save();
				}
			}

			$this->Application->DeleteLanguageCache();
			$this->gotoPage('Admin.LanguageMan');
		}
	}

	public function loadlangfile($filename)
	{
		$XML = simplexml_load_file($filename);

		if(!$XML)
			return false;

		$langstrings = $XML->language;
		$attr	= $langstrings->attributes();
		$this->_culture = (string)$attr->culture;
		$translationStrings = $XML->xpath('//string');
		$translations = array();

		foreach($translationStrings as $string)
		{
			$attr = $string->attributes();
			$translations[] = array((string)$attr->id,(string)$attr->lkey,(string)$string->fromvalue,(string)$string->tovalue);
		}
		return $translations;
	}
}
?>